package FIT::Document::Parser::t::Twig;

use utf8;
use FIT::Test 'FIT::Document::Parser::Twig';
use aliased 'FIT::Document::Parser::Text';
use FIT::Util::HTMLBuilder;

# test utility: parse the HTML and return the element of the 1st table
sub first_table()
    { IUT->make_twig->parse( Text->prepare_for_read(pop) )->root->first_table }

sub fixture_name: Test
    { is first_table( HTML TABLE TR TD 'foo' )->fixture_name, 'foo' }

sub next_table: Test {
    is
        first_table( HTML TABLE(TR TD 'a'), TABLE(TR TD 'b') )
            ->next_table->fixture_name,
        'b';
}

sub fixture_args: Test {
    is_deeply
        [
            first_table(
                HTML TABLE TR(
                    TD('a'), TD('b'), TD('c')
                )
            )->fixture_args
        ],
        [qw(b c)];
}

sub column_index: Test {
    is
        first_table( HTML TABLE TR( TD('a'), TD('b') ) )
            ->first_cell->next_cell->column_index,
        1;
}

sub cell_above: Test {
    is
        first_table( HTML TABLE( (TR TD 'a'), (TR TD 'b') ) )
            ->second_row->first_cell->cell_above->fit_text,
        'a';
}

sub undef_when_element_not_found: Test
    { is first_table( HTML TABLE TR TD 'a' )->next_table, undef }

sub fit_text: Test {
    is
        first_table( HTML TABLE TR TD 'a<br/>b&nbsp;c ' )->first_cell->fit_text,
        "a\nb c";
}

sub arbitary_bytes: Test {
    is
        first_table( HTML TABLE TR TD 'βFFβ11μ03b2' )->first_cell->fit_text,
        "\xFF\x11\x{03B2}";
}

sub column_headings: Test(2) {
    my $table = first_table(
        HTML TABLE(
            TR( TD 'foo' ),
            TR( TD('a'), TD('b'), TD(''), TD('c') ),
            TR( TD('1'), TD('2'), TD(''), TD('3') ),
        )
    );  
    is_deeply
        [$table->column_headings],
        ['a', 'b', '', 'c'],
        'column headings';
    is
        $table->second_row->next_row->first_cell->next_cell->column_heading,
        'b',
        'column heading';
}

sub get_body_no_body: Test {
    my $table = first_table(HTML TABLE TR TD 'bar');
    $table->get_body;
    is $table->sprint, '<table><tbody><tr><td>bar</td></tr></tbody></table>';
}

sub get_body_with_body: Test
    { is first_table(HTML TABLE TBODY TR TD 'foo')->get_body->tag, 'tbody' }

sub is_empty_false: Test
    { ok !first_table(HTML TABLE TBODY TR TD 'xxx')->first_cell->fit_is_empty }

sub is_empty_true: Test
    { ok first_table(HTML TABLE TBODY TR TD "\n<x><br/>&nbsp; </x>")->first_cell->fit_is_empty }

sub fit_identifier: Test
    { is first_table(HTML TABLE BODY TR TD "a b  c")->first_cell->fit_identifier, 'a_b_c' }

sub document_body_existing: Test
    { is first_table(HTML BODY TABLE BODY TR TD "x")->document_body->tag, 'body' }

sub document_body_created: Test
    { is first_table(HTML TABLE BODY TR TD "x")->document_body->tag, 'body' }
